//
//  UIButton+Mybutton.m
//  QuickCoin
//
//  Created by tjl on 16/11/22.
//  Copyright © 2016年 tjl. All rights reserved.
//

#import "UIButton+Mybutton.h"
#import "config.h"
#import "ColorUtil.h"
#import "UILabel+MyLabel.h"

@implementation UIButton (Mybutton)
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect bounds = self.bounds;
    //若原热区小于44x44，则放大热区，否则保持原大小不变
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}
+ (UIButton *)createButtonWithFrame:(CGRect)frame NormalImage:(UIImage *)normal selectedImage:(UIImage *)selected target:(id)target andSEL:(SEL)action {
    
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    
    [button setImage:normal forState:UIControlStateNormal];
    [button setImage:selected forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.clipsToBounds = YES;
    
    button.layer.cornerRadius = 2;
    
    return button;
    
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame andTitle:(NSString *)title andLayer:(float)ishav andtraget:(id)traget andAction:(SEL)action backColor:(id)backColor {
	
	UIButton *button = [self createButtonWithFrame:frame andTitle:title andLayer:ishav andtraget:traget andAction:action];
	button.backgroundColor = backColor;
	
	return button;
	
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame andTitle:(NSString *)title andLayer:(float)ishav andtraget:(id)traget andAction:(SEL)action {
    
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    if (ishav>0) {
        button.clipsToBounds = YES;
        
        button.layer.cornerRadius = ishav;
    }
    [button addTarget:traget action:action forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont fontWithName:kFontNameLight size:labelMiddleFont];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithWhite:1 alpha:0.4] forState:UIControlStateDisabled];
    
    return button;
    
}

/**
 *  创建button
 *
 *  @return button
 */
+ (UIButton *)createButtonWithTitle:(NSString *)titile frame:(CGRect)frame target:(id)target andSEL:(SEL)action font:(float)font textColor:(id)textColor {
    
    UIButton *btn = [self createButtonWithTitle:titile frame:frame target:target andSEL:action font:font];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    return btn;
}
+ (UIButton *)createButtonWithTitle:(NSString *)titile frame:(CGRect )frame target:(id)target andSEL:(SEL)action font:(float)font
{
    return [self createButtonWithTitle:titile frame:frame target:target andSEL:action font:font board:NO];
}
+ (UIButton *)createButtonWithTitle:(NSString *)titile frame:(CGRect )frame target:(id)target andSEL:(SEL)action font:(float)font board:(BOOL)board
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:titile forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[ColorUtil white] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont fontWithName:board?kFontNameBold:kFontNameLight size:font];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}
/**
 *  创建底部圆角button
 *
 *  @return button
 */
+ (UIButton *)createBottomButtonWithTitle:(NSString *)titile frame:(CGRect )frame target:(id)target andSEL:(SEL)action font:(float)font {
    
    UIButton *btn = [self createButtonWithTitle:titile frame:frame target:target andSEL:action font:font board:NO];
    btn.backgroundColor = kMainColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 4;
    
    return btn;
}
+ (UIButton *)createCirleButtonWithFrame:(CGRect )frame title:(NSString *)title target:(id)target SEL:(SEL)action andCircleColor:(id)color {
    
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    
    [button setTitle:title forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateSelected];
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 4;
    button.layer.borderWidth = kLineWitdth;
    button.layer.borderColor = [color CGColor];
    
    button.titleLabel.font = [UIFont fontWithName:kFontNameLight size:labelMiddleFont-2];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
    
}

+ (UIButton *)createButtonWithLeftImage:(UIImage *)leftImage rightTitle:(NSString *)title target:(id)target SEL:(SEL)action andFrame:(CGRect)frame {
    
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setBackgroundColor:[UIColor whiteColor]];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat width = 40;
    CGFloat height = 35;
    CGFloat x = (frame.size.width - width)/2;
    CGFloat y = (frame.size.height - height-25)/2;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    image.image = leftImage;
    [button addSubview:image];
    
    UILabel *right = [UILabel createLabelWithFrame:CGRectZero title:title textColor:kText_color isCenter:YES font:labelMiddleFont andHaveUnderLine:NO board:NO];
    [button addSubview:right];
    
    right.frame = CGRectMake(0, CGRectGetMaxY(image.frame)+5, frame.size.width, 20);
    return button;
}

+ (UIView *)configBottomButtonWithTarget:(id)target Sel:(SEL)section title:(NSString *)title {

    UIView *bottomView = [self configBottomButtonWithTarget:target Sel:section title:title bgColor:kMainColor];

	return bottomView;
	
}
+ (UIView *)configBottomButtonWithTarget:(id)target Sel:(SEL)section title:(NSString *)title bgColor:(id)bgColor
{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, DeviceHeight-82-kIs_iPhoneX_height, DeviceWidth, 82)];
    bottomView.backgroundColor = [UIColor whiteColor];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, kLineWitdth)];
    lineView.backgroundColor = kLineColor;
    [bottomView addSubview:lineView];
    
    UIButton *bottom = [UIButton createButtonWithTitle:title frame:CGRectMake(20, CGRectGetMaxY(lineView.frame)+(70-40)/2, DeviceWidth-20*2, 40) target:target andSEL:section font:kLargeFont];
    bottom.backgroundColor = bgColor;
    bottom.clipsToBounds = YES;
    bottom.layer.cornerRadius = 4;
    [bottomView addSubview:bottom];
    return bottomView;
}


@end

@implementation UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
